//
//  JCReportStatisticCell.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/30.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCReportStatisticCell.h"
#import "NSDate+CM_DateTools.h"
#import "JCReportModel.h"

@interface JCReportStatisticCell ()

@property (weak, nonatomic) IBOutlet UILabel *swingTotalValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingTotalTrendLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingAverageValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *swingAverageTrendLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeTotalValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeTotalTrendLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeAverageValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeAverageTrendLabel;

@property (weak, nonatomic) IBOutlet UILabel *calTotalValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *calTotalTrendLabel;
@property (weak, nonatomic) IBOutlet UILabel *calAverageValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *calAverageTrendLabel;

@end

@implementation JCReportStatisticCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setReportModel:(JCReportModel *)reportModel{
    _reportModel = reportModel;

    NSInteger swingTotal = reportModel.weekTotalModel.swings.integerValue;
    NSInteger swingAverage = reportModel.weekTotalModel.avgSwings.integerValue;
    NSInteger timeTotal = reportModel.weekTotalModel.duration.integerValue;
    NSInteger timeAverage = reportModel.weekTotalModel.avgDuration.integerValue;
    NSInteger calTotal = reportModel.weekTotalModel.caroline.integerValue;
    NSInteger calAverage = reportModel.weekTotalModel.avgCaroline.integerValue;
    
    NSInteger lastSwingTotal = reportModel.lastWeekTotalModel.swings.integerValue;
    NSInteger lastSwingAverage = reportModel.lastWeekTotalModel.avgSwings.integerValue;
    NSInteger lastTimeTotal = reportModel.lastWeekTotalModel.duration.integerValue;
    NSInteger lastTimeAverage = reportModel.lastWeekTotalModel.avgDuration.integerValue;
    NSInteger lastCalTotal = reportModel.lastWeekTotalModel.caroline.integerValue;
    NSInteger lastCalAverage = reportModel.lastWeekTotalModel.avgCaroline.integerValue;
    
    NSInteger swingTotalScale = lastSwingTotal?((CGFloat)swingTotal / (CGFloat)lastSwingTotal) * 100:-100;
    NSInteger swingAverageScale = lastSwingAverage?((CGFloat)swingAverage / (CGFloat)lastSwingAverage) * 100:-100;
    NSInteger timeTotalScale = lastTimeTotal?((CGFloat)timeTotal / (CGFloat)lastTimeTotal) * 100:-100;
    NSInteger timeAverageScale = lastTimeAverage?((CGFloat)timeAverage / (CGFloat)lastTimeAverage) * 100:-100;
    NSInteger calTotalScale = lastCalTotal?((CGFloat)calTotal / (CGFloat)lastCalTotal) * 100:-100;
    NSInteger calAverageScale = lastCalAverage?((CGFloat)calAverage / (CGFloat)lastCalAverage) * 100:-100;
    
    _swingTotalValueLabel.text = @(swingTotal).stringValue;
    [self trendStringForScale:swingTotalScale label:_swingTotalTrendLabel];
    _swingAverageValueLabel.text = @(swingAverage).stringValue;
    [self trendStringForScale:swingAverageScale label:_swingAverageTrendLabel];
    
    _timeTotalValueLabel.text = [NSDate convertToHourMinFormateWithMin:timeTotal];
    [self trendStringForScale:timeTotalScale label:_timeTotalTrendLabel];
    _timeAverageValueLabel.text = [NSDate convertToHourMinFormateWithMin:timeAverage];
    [self trendStringForScale:timeAverageScale label:_timeAverageTrendLabel];
    
    _calTotalValueLabel.text = @(calTotal).stringValue;
    [self trendStringForScale:calTotalScale label:_calTotalTrendLabel];
    _calAverageValueLabel.text = @(calAverage).stringValue;
    [self trendStringForScale:calAverageScale label:_calAverageTrendLabel];
}

- (void)trendStringForScale:(NSInteger)scaleValue label:(UILabel *)label{
    UIColor *color;
    NSString *string;
    if (scaleValue == -100){
        color = [UIColor redColor];
        string = @"↑";
    }else if (scaleValue > 100) {
        color = [UIColor redColor];
        string = [NSString stringWithFormat:@"+%ld%% ↑",scaleValue-100];
    }else if (scaleValue == 100){
        color = UIColorFromHex(0x999999);
        string = @"--";
    }else if (scaleValue < 100){
        color = [UIColor greenColor];
        string = [NSString stringWithFormat:@"-%ld%% ↓",100-scaleValue];
    }
    label.textColor = color;
    label.text = string;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
